import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Element;
import javax.swing.text.Highlighter;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.tree.DefaultMutableTreeNode;
import org.antlr.runtime.Token;

/**
 * 
 * @author Padda1
 */
public class GUI extends javax.swing.JFrame 
{
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    String view = "0";
    boolean treeSelectedBefore = false;
    boolean dragging = false;
    final int PROX_DIST = 3;
    String selectedClassName = " ";
    public static String word;
    public static Highlighter highlighter = new TextHighlighter(null);
    WordSearcher searcher;
    File file;//The file used when opening and saving
    FileEditor editor = new FileEditor();
    Vector<String> resultList = new Vector<String>();
    Vector<Token> list = new Vector<Token>();   
    JFileChooser chooser;
    String result = "";
    File dir;
    File[] files;
    DefaultListModel listModel;
    DefaultTreeModel treeModel;
    String location;
    String properLocation;
    String javaLocation;
    String workingFolder = "";
    double finalTime;
    
    /** 
     * Creates new form GUI 
     */
    public GUI() 
    {
        location = "";
        chooser = new JFileChooser();
        
        String workingDir = "";
        try 
        {
            workingDir = new File(".").getCanonicalFile().toURI().getPath().substring(1);
            dir = new File(workingDir + "Refactoring Methods");
        }
        catch (IOException ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "File Not Found", JOptionPane.ERROR_MESSAGE);
        }
        
        files = dir.listFiles(new WildCardFileFilter("*.java"));
        
        FileEditor viewFile = new FileEditor();
        try 
        {
            view = viewFile.read("src/viewSelection.cfg").trim();
        }
        catch (FileNotFoundException ex) 
        {
            JOptionPane.showMessageDialog(null, "viewSelection.cfg was not found.", "File Not Found", JOptionPane.ERROR_MESSAGE);
        }
        
        //Get white out of pic and size to 256x256 png format
        //Change pic in all forms.
        Image icon = Toolkit.getDefaultToolkit().getImage("src/infinity_sign_small.jpg");
        setIconImage(icon);
        
        initComponents();
        
        jButton1.setEnabled(false);
        jButton2.setEnabled(false);
        jTree1.setEnabled(false);
        listModel = new DefaultListModel();
        jList1.setModel(listModel);
        RefactoringProxy proxy = new MethodProxy(jComboBox1.getSelectedItem().toString().replace(" ", "_"));
        jLabel1.setText("Description: " + proxy.description());
        jLabel1.setToolTipText(proxy.description());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jSplitPane2 = new javax.swing.JSplitPane();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CASE-Tool");
        setBackground(new java.awt.Color(255, 255, 255));
        setForeground(java.awt.Color.blue);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/infinity_sign_small.jpg"))); // NOI18N
        jLabel2.setText("jLabel2");

        titleLabel.setFont(new java.awt.Font("Tempus Sans ITC", 3, 36)); // NOI18N
        titleLabel.setForeground(new java.awt.Color(0, 153, 204));
        titleLabel.setText("CASE-Tool for Code Refactoring");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(102, 102, 102)
                .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 729, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(143, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
                .addContainerGap())
        );

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/open_file-icon.gif"))); // NOI18N
        jButton3.setText("Open Project");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        //////////////////////////////Populate ComboBox//////////////////////////////
        Compiler compiler = new Compiler();
        if (files.length < 1)
        {
            jComboBox1.addItem("<Refactoring methods here>");
        }
        else
        {
            for (File file : files)
            {
                try
                {
                    boolean worked = compiler.compile(file.getCanonicalFile().toURI().getPath().substring(1));
                    if (worked == true)
                    {
                        jComboBox1.addItem(file.getName().substring(0, file.getName().length() - 5).replace("_", " "));
                    }
                }
                catch (IOException ex)
                {
                    JOptionPane.showMessageDialog(null, ex.toString(), "File Not Found", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        if (jComboBox1.getItemCount() < 1)
        {
            jComboBox1.addItem("<Refactoring methods here>");
        }

        //////////////////////////////Populate ComboBox//////////////////////////////
        jComboBox1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButton1.setText("Refactor");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Preview");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Description: This describes the current refactoring method. How it is done and what it will do!");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 678, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jSplitPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jSplitPane1.setDividerLocation(150);

        jTree1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.setDoubleBuffered(true);
        jTree1.setEnabled(false);
        jTree1.setRootVisible(false);
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jSplitPane2.setBorder(null);
        jSplitPane2.setDividerLocation(400);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTabbedPane1MouseClicked(evt);
            }
        });

        jLabel4.setText("Welcome to the CASE-Tool for code refactoring. Select browse or open to open a project directory and start refactoring.");

        jLabel5.setText("To begin click open project");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(337, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addContainerGap(327, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Startup", jPanel2);

        jSplitPane2.setTopComponent(jTabbedPane1);

        jLabel6.setText("Time taken for refactoring method to complete:");

        jScrollPane2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jList1.setModel(new DefaultListModel());
        jScrollPane2.setViewportView(jList1);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addContainerGap(695, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 932, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
        );

        jSplitPane2.setRightComponent(jPanel4);

        jSplitPane1.setRightComponent(jSplitPane2);

        jMenu1.setText("File");
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jMenu1MouseExited(evt);
            }
        });

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Open Project");
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jMenuItem1MouseExited(evt);
            }
        });
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem2.setText("Exit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Find");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Preferences");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuBar1.add(jMenu2);

        jMenu4.setText("Help");

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem4.setText("Help Contents");
        jMenu4.add(jMenuItem4);

        jMenuItem6.setText("About");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem6);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 1090, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 507, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1126)/2, (screenSize.height-768)/2, 1126, 768);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Preview Button
     * @param evt 
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        int listPos = jList1.getModel().getSize();
        listModel.add(listPos, jComboBox1.getSelectedItem().toString());
        
        long beginning = System.currentTimeMillis();

        Compiler compiler = new Compiler();
        boolean compiled = compiler.compile(properLocation+"/"+selectedClassName);

        if(compiled)
        {
            File userDir = new File(properLocation);
            File[] userFiles = userDir.listFiles(new WildCardFileFilter("*.java"));
            
            for(int x = 0; x < userFiles.length; x++)
            {
                try 
                {
                    editor.write("tempDir/" + userFiles[x].getName(), editor.read(userFiles[x].getAbsolutePath()));
                }
                catch (IOException ex) 
                {
                    JOptionPane.showMessageDialog(null,ex.toString(), "I/O Error", JOptionPane.ERROR_MESSAGE);
                }
            }
            
            String className = jComboBox1.getSelectedItem().toString().replace(" ", "_");

            Vector<String> parameters = new Vector<String>();
            parameters.add(properLocation+"/"+selectedClassName);
            parameters.add(null);
            parameters.add(null);
            
            RefactoringProxy proxy = new MethodProxy(className);
            resultList = proxy.refactor(parameters);

            StringTokenizer st = new StringTokenizer(resultList.get(1)," ");
            if("Error,".equals(st.nextToken()))
            {
                JOptionPane.showMessageDialog(null,resultList.get(1), "Refactoring Error", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                StringTokenizer token = new StringTokenizer(resultList.get(0),"/");
                String tempPathName = "";
                while(token.hasMoreTokens())
                {
                    tempPathName = token.nextToken();
                }                
                try
                {
                    editor.write("tempDir/"+tempPathName, resultList.get(1));
                }
                catch (IOException ex) 
                {
                    JOptionPane.showMessageDialog(null, ex.toString(), "I/O Error", JOptionPane.ERROR_MESSAGE);
                }
                
                JOptionPane.showMessageDialog(null, "Successfully Refactored", "Success", JOptionPane.INFORMATION_MESSAGE);
                
                if(isTabOpen(selectedClassName))
                {
                    jTabbedPane1.remove(jTabbedPane1.indexOfTab(selectedClassName));
                    jTabbedPane1.addTab(selectedClassName, display(selectedClassName,tempPathName,view));
                    jTabbedPane1.setSelectedIndex(jTabbedPane1.getTabCount() - 1);
                }
                else
                {
                    jTabbedPane1.addTab(selectedClassName, display(selectedClassName,resultList.get(1),view));
                }//else tab is not open
            }//else not an error
        }//if compiled
        else
        {
            JOptionPane.showMessageDialog(null, selectedClassName + " compiled with errors.", "Compile Error", JOptionPane.ERROR_MESSAGE);
        }
        
        File tempDir = new File("tempDir");
        File[] tempFiles = tempDir.listFiles();
        for(int x = 0; x < tempFiles.length; x++)
        {
            tempFiles[x].delete();
        }
        
        long end = System.currentTimeMillis() - beginning;
        finalTime = end/1000.000;
        listPos = jList1.getModel().getSize() -1;   
        
        for (int x = 0; x < listPos; x++)
        {
            if(listModel.getElementAt(x).toString().contains(jComboBox1.getSelectedItem().toString()))
            {
                listModel.set(listPos, (String) listModel.get(x) + ", " + finalTime + "s");
                listModel.remove(x);
            }            
        }
        listModel.set(listPos, (String) listModel.get(listModel.getSize()-1) + " " + finalTime + "s");
        
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * Combobox
     * @param evt 
     */
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        
        RefactoringProxy proxy = new MethodProxy(jComboBox1.getSelectedItem().toString().replace(" ", "_"));
        String description = proxy.description() + " ";
        String newdescription = "";
        String before = "";
        String after = "";
        if(description.length() > 100)
        {
            before = description.substring(0,description.indexOf(" ", 100));
            after = description.substring(description.indexOf(" ", 100));
            newdescription = before + "<br></br>      " + after;
        }
        jLabel1.setText("<html><body>Description: "+newdescription+"</body></html>");
        jLabel1.setToolTipText(proxy.description());
    }//GEN-LAST:event_jComboBox1ActionPerformed

    /**
     * Exit Menu Item
     * @param evt 
     */
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * Browse Button
     * @param evt 
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        openPath();
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * Open Menu Item
     * @param evt 
     */
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        openPath();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * About Menu Item
     * @param evt 
     */
    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        new About().setVisible(true);
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    /**
     * Find Menu Item
     * @param evt 
     */
    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
       findFrame();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    /**
     * Options Menu Item
     * @param evt 
     */
    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
       Options opt = new Options();
       opt.setVisible(true);
       view = opt.getSelected();
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    /**
     * JTree Selected
     * @param evt 
     */
    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        int x = 0;
        boolean alreadyOpen = false;
        if (treeSelectedBefore == true)
        {   
            getClassName();
            while (x < jTabbedPane1.getTabCount())
            {
                if (jTabbedPane1.getTitleAt(x).equals(selectedClassName))
                {
                    alreadyOpen = true;
                }
                x++;
            }
            if (alreadyOpen == false)
            {
                jTabbedPane1.addTab(selectedClassName, display(selectedClassName, "", view));
                jTabbedPane1.setSelectedIndex(jTabbedPane1.getTabCount() - 1);
            }
        }
        else
        {
            getClassName();
            jTabbedPane1.addTab(selectedClassName, display(selectedClassName, "", view));
            jTabbedPane1.remove(0);
            treeSelectedBefore = true;
        }
    }//GEN-LAST:event_jTree1ValueChanged
    
    /**
     * Refactor button pressed.
     * @param evt 
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int overwrite = JOptionPane.showConfirmDialog(new JFrame(), "Are you sure you want do refactor? Changes cannot be undone. Source files will be changed.\n", "Overwrite?", JOptionPane.YES_NO_OPTION);
        if (overwrite == 0) 
        {
            try 
            {
                for (int i = 0; i < resultList.size(); i+=2)
                {
                    editor.write(resultList.get(i), resultList.get(i+1));
                }
            } 
            catch (IOException ex) 
            {
                JOptionPane.showMessageDialog(null, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTabbedPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPane1MouseClicked
       if (evt.getButton() == MouseEvent.BUTTON1)
       {
           selectedClassName = jTabbedPane1.getTitleAt(jTabbedPane1.getSelectedIndex());
       }
       if (evt.getButton() == MouseEvent.BUTTON3)
       {
            JPopupMenu menu = new JPopupMenu();
           
            JMenuItem close = new JMenuItem("Close");
            close.addActionListener(new java.awt.event.ActionListener() 
            {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent evt) 
                {                    
                    jTabbedPane1.remove(jTabbedPane1.getSelectedIndex());
                }                        
            });
            menu.add(close);
            menu.show(jTabbedPane1, evt.getX(), evt.getY()); 
           //jTabbedPane1.remove(jTabbedPane1.getSelectedIndex());
       }
    }//GEN-LAST:event_jTabbedPane1MouseClicked

    private void jMenu1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu1MouseExited
        //jMenu1.setPopupMenuVisible(false);
    }//GEN-LAST:event_jMenu1MouseExited

    private void jMenuItem1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MouseExited
        //jMenu1.setPopupMenuVisible(false);
    }//GEN-LAST:event_jMenuItem1MouseExited
    
    /**
     * Gets the class selected form the JTree.
     */
    void getClassName()
    {
        String path = "";
        StringTokenizer token = new StringTokenizer(jTree1.getSelectionPath().toString(),"[]");
        while(token.hasMoreTokens()) 
        {
            path+=token.nextToken();
        }
        StringTokenizer token2 = new StringTokenizer(path, ", ");
        while(token2.hasMoreTokens()) 
        {
            selectedClassName=token2.nextToken();
        }
    }
    
    /** Small routine that will make node out of the first entry
    *  in the array, then make nodes out of subsequent entries
    *  and make them child nodes of the first one. The process is
    *  repeated recursively for entries that are arrays.
    */
    private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) 
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
        DefaultMutableTreeNode child;
        for(int i=1; i<hierarchy.length; i++) 
        {
            Object nodeSpecifier = hierarchy[i];
            if (nodeSpecifier instanceof Object[])
            {
                child = processHierarchy((Object[])nodeSpecifier);
            }
            else 
            {
                child = new DefaultMutableTreeNode(nodeSpecifier);
            } // Ie Leaf
            node.add(child);
        }
        return(node);
    }
    
    /**
     * Open Dialog and gets file paths.
     */
    void openPath()
    {
        if(!jTree1.isSelectionEmpty())
        {
            jTree1.setSelectionRow(-1);
        }
        
        jTree1.setEnabled(true);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        int diag = chooser.showOpenDialog(null);
        try
        {
            if(diag == JFileChooser.APPROVE_OPTION)
            {
                file = chooser.getSelectedFile();
            }
            else if (diag == JFileChooser.CANCEL_OPTION)
            {
                return;
            }            
            location = file.getPath();
            properLocation = pathConversion(location);            
            //String workingFolder = "";
            StringTokenizer workingFolderToken = new StringTokenizer(properLocation,"/");
            
            while(workingFolderToken.hasMoreTokens())
            {
                workingFolder = workingFolderToken.nextToken();
            }
            
            File path;
            File[] treeFiles;
            String[] treeFilesNames = {""};
            if (workingFolder.contains(".java"))
            {
                location = file.getParent();
                properLocation = pathConversion(location);            
                System.out.println(properLocation);           
                treeFiles = new File[2];
                treeFilesNames = new String[treeFiles.length];                
                treeFilesNames[0] = "";
                treeFilesNames[1] = file.getName();
            }
            else
            {
                path = new File(properLocation);            
                treeFiles = path.listFiles(new WildCardFileFilter("*.java"));
                treeFilesNames = new String[treeFiles.length + 1];
                treeFilesNames[0] = workingFolder;
                int cnt = 0;
                while (cnt < treeFiles.length)
                {
                    treeFilesNames[cnt+1] = treeFiles[cnt].getName();
                    cnt++;
                }
            }
            
            //File[] treeFiles = file.listFiles(new WildCardFileFilter("*.java"));
            DefaultMutableTreeNode root = processHierarchy(treeFilesNames);
            treeModel = new DefaultTreeModel(root);
            jTree1.setModel(treeModel);            
        }
        catch(NullPointerException nullPointer)
        {
            JOptionPane.showMessageDialog(null, nullPointer.toString(), "Null Pointer Exception", JOptionPane.ERROR_MESSAGE);
        }
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
    }
    
    /**
     * Converts a given path from <folder>\<folder>\<file> to <folder>/<folder>/<file>
     * @param loc String. The location to convert.
     * @return String. The converted location.
     */
    String pathConversion(String loc)
    {
        String propLoc = "";
        StringTokenizer token = new StringTokenizer(loc,"\\");
        propLoc = token.nextToken();
        while (token.hasMoreTokens())    
        {
            propLoc = propLoc + "/" + token.nextToken();
        }
        return propLoc;
    }
    
    /**
     * Creates the SplitPane to display in tab.
     * @param before String. The file to display in the before view.
     * @param after String. The file to display in the after view.
     * @param view1 Sting. The view style: 0 - vertical, 1 - horizontal
     * @return JPanel. Returns a panel with the before and after text panes.
     */
    JSplitPane display(String before, String after, String view1)
    {
        JSplitPane pane = new JSplitPane();
        pane.setDividerLocation(jTabbedPane1.getWidth()/2);
        
        JPanel beforePanel = new JPanel();
        JPanel afterPanel = new JPanel();
        
        JLabel beforeLabel = new JLabel("Before:");
        JLabel afterLabel = new JLabel("After:");
        
        JScrollPane beforePane = new JScrollPane();
        JScrollPane afterPane = new JScrollPane();
        
        final JTextPane beforeTextPane = new JTextPane()
        {
            @Override
            public boolean getScrollableTracksViewportWidth() 
            {
                return (getSize().width < getParent().getSize().width);
            }
            @Override
            public void setSize(Dimension d) 
            {
                if (d.width < getParent().getSize().width) 
                {
                    d.width = getParent().getSize().width;
                }
                super.setSize(d);
            }
        };                
        final JTextPane afterTextPane = new JTextPane()
        {
            @Override
            public boolean getScrollableTracksViewportWidth() 
            {
                return (getSize().width < getParent().getSize().width);
            }
            @Override
            public void setSize(Dimension d) 
            {
                if (d.width < getParent().getSize().width) 
                {
                    d.width = getParent().getSize().width;
                }
                super.setSize(d);
            }
        };
        
        final JTextArea beforeNumbers = new JTextArea();
        final JTextArea afterNumbers = new JTextArea();
        
        beforeTextPane.setEditable(false);
        afterTextPane.setEditable(false);
        
        beforeNumbers.setFont(beforeTextPane.getFont());
        afterNumbers.setFont(afterTextPane.getFont());
        
        beforeNumbers.setBackground(Color.LIGHT_GRAY);
        beforeNumbers.setEditable(false);
        beforePane.setRowHeaderView(beforeNumbers);
        
        afterNumbers.setBackground(Color.LIGHT_GRAY);
        afterNumbers.setEditable(false);
        afterPane.setRowHeaderView(afterNumbers);

        Vector<Token> colorTree = new Vector<Token>();
        ParserProxy parser = new ParserProxy("FileParser");
        colorTree = parser.parseColor(properLocation + "/" + before);
        try
        {
            beforeTextPane.read(new FileReader(properLocation+"/"+before), null);
            BufferedReader b = new BufferedReader(new FileReader(properLocation+"/"+before));
            StyledDocument doc = beforeTextPane.getStyledDocument();
            searcher = new WordSearcher(beforeTextPane);
            int x = 0;
            int total = 0;
            Vector<keyWords> key = new Vector<keyWords>();
            while (x < colorTree.size())
            {
                if (colorTree.get(x).getText().equals("abstract") || colorTree.get(x).getText().equals("interface") || colorTree.get(x).getText().equals("this") || colorTree.get(x).getText().equals("THROWS_CLAUSE") || colorTree.get(x).getText().equals("char") || colorTree.get(x).getText().equals("IMPLEMENTS_CLAUSE") || colorTree.get(x).getText().equals("EXTENDS_CLAUSE") ||colorTree.get(x).getText().equals("final") || colorTree.get(x).getText().equals("final") || colorTree.get(x).getText().equals("static") || colorTree.get(x).getText().equals("byte") || colorTree.get(x).getText().equals("break") || colorTree.get(x).getText().equals("try") || colorTree.get(x).getText().equals("catch") || colorTree.get(x).getText().equals("false") || colorTree.get(x).getText().equals("true") || colorTree.get(x).getText().equals("while") || colorTree.get(x).getText().equals("for") || colorTree.get(x).getText().equals("if") || colorTree.get(x).getText().equals("boolean") || colorTree.get(x).getText().equals("new") || colorTree.get(x).getText().equals("int") || colorTree.get(x).getText().equals("import") || colorTree.get(x).getText().equals("return") || colorTree.get(x).getText().equals("class") || colorTree.get(x).getText().equals("extends") || colorTree.get(x).getText().equals("public") || colorTree.get(x).getText().equals("private") || colorTree.get(x).getText().equals("package"))
                {
                    int length = colorTree.get(x).getText().length();
                    if(colorTree.get(x).getText().equals("THROWS_CLAUSE"))
                    {
                        length = 6;
                    }
                    if(colorTree.get(x).getText().equals("EXTENDS_CLAUSE"))
                    {
                        length = 7;
                    }
                    if(colorTree.get(x).getText().equals("IMPLEMENTS_CLAUSE"))
                    {
                        length = 10;
                    }
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(0);
                    obj.setG(0);
                    obj.setB(225);
                    obj.setColor("Blue");
                    key.add(obj);
                }
                if (colorTree.get(x).getText().equals("STATIC_ARRAY_CREATOR"))
                {
                    int length = 3;
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(0);
                    obj.setG(0);
                    obj.setB(225);
                    obj.setColor("Blue");
                    key.add(obj);
                }
                if (colorTree.get(x).getText().equals("throw") && colorTree.get(x).getType() == 96)
                {
                    int length = colorTree.get(x).getText().length();
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(0);
                    obj.setG(0);
                    obj.setB(225);
                    obj.setColor("Blue");
                    key.add(obj);
                }
                if (colorTree.get(x).getType() == 164 && colorTree.get(x-1).getText().equals("out") && ((x-1) > -1) && colorTree.get(x-1).getText().equals("."))
                {
                    int length = 3;
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(0);
                    obj.setG(0);
                    obj.setB(225);
                    obj.setColor("Blue");
                    key.add(obj);
                }
                if (colorTree.get(x).getType() == 164 && !colorTree.get(x).getText().equals("String")&& !colorTree.get(x).getText().equals("System") && (x-1) > 1 && !colorTree.get(x-1).getText().equals("MODIFIER_LIST") && !colorTree.get(x-1).getText().equals("import") && !colorTree.get(x-1).getText().equals("FORMAL_PARAM_LIST") && !colorTree.get(x-1).getText().equals("LOCAL_MODIFIER_LIST") && !colorTree.get(x-1).getText().equals("EXP") && !colorTree.get(x-1).getText().equals("FUNCTION_METHOD_DECL") && !colorTree.get(x-1).getText().equals("VOID_METHOD_DECL") && !colorTree.get(x-1).getText().equals("class") && !colorTree.get(x-1).getText().equals(".") && !colorTree.get(x-1).getText().equals("@"))
                {
                    int length = colorTree.get(x).getText().length();
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(0);
                    obj.setG(201);
                    obj.setB(87);
                    obj.setColor("Green");
                    key.add(obj);
                }
                if (colorTree.get(x).getText().equals("VOID_METHOD_DECL"))
                {
                    int length = 4;
                    int startPosition = colorTree.get(x+1).getCharPositionInLine() - 5;
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x+1).getLine());
                    obj.setR(0);
                    obj.setG(0);
                    obj.setB(225);
                    obj.setColor("Blue");
                    key.add(obj);
                }
                if (colorTree.get(x).getText().equals("out"))
                {
                    int length = colorTree.get(x).getText().length();;
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(0);
                    obj.setG(201);
                    obj.setB(87);
                    obj.setColor("Green");
                    key.add(obj);
                }
                if (colorTree.get(x).getType() == 164 && (x-1) > -1 && colorTree.get(x-1).getText().equals("VOID_METHOD_DECL"))
                {
                    int length = colorTree.get(x).getText().length();;
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(3);
                    obj.setG(3);
                    obj.setB(3);
                    obj.setColor("Black");
                    key.add(obj);
                }
                if (colorTree.get(x).getText().charAt(0) == '\"')
                {
                    int length = colorTree.get(x).getText().length();
                    int startPosition = colorTree.get(x).getCharPositionInLine();
                    keyWords obj = new keyWords();
                    obj.setColumnNum(startPosition);
                    obj.setLength(length);
                    obj.setLineNum(colorTree.get(x).getLine());
                    obj.setR(225);
                    obj.setG(171);
                    obj.setB(36);
                    obj.setColor("Chocolate 1");

                    key.add(obj);
                }
                x++;
            }
            keyWords ki = new keyWords();
            key = ki.sort(key);
            String sa = "";
            int counte = 0;
            total = 0;
            int df = 0;
            while((sa = b.readLine()) != null)
            {
                if(df < key.size() && counte==key.get(df).getLineNum()-1)
                {
                    Color color = new Color(key.get(df).getR(),key.get(df).getG(),key.get(df).getB());
                    Style style;
                    style = beforeTextPane.addStyle(key.get(df).getColor(), null);
                    StyleConstants.setForeground(style, color);
                    doc.setCharacterAttributes(total+key.get(df).getColumnNum(), key.get(df).getLength(), beforeTextPane.getStyle(key.get(df).getColor()), true);

                    df++;
                    while(df < key.size() && key.get(df).getLineNum()== key.get(df-1).getLineNum())
                    {
                        color = new Color(key.get(df).getR(),key.get(df).getG(),key.get(df).getB());
                        style = beforeTextPane.addStyle(key.get(df).getColor(), null);
                        StyleConstants.setForeground(style, color);
                        doc.setCharacterAttributes(total+key.get(df).getColumnNum(), key.get(df).getLength(), beforeTextPane.getStyle(key.get(df).getColor()), true);
                        df++;
                    }
                }
                counte += 1;
                if(sa.equals(""))
                {
                    total += 1;
                }
                else
                {
                    total += sa.length()+1;
                }
            }
            if (after.equals(""))
            {
                afterTextPane.setText(after);
            }
            else
            {
                afterTextPane.read(new FileReader("tempDir/"+after), null);
                doc = afterTextPane.getStyledDocument();
                b = new BufferedReader(new FileReader("tempDir/"+after));
                x = 0;
                total = 0;
                colorTree = parser.parseColor("tempDir/"+after);
                key = new Vector<keyWords>();
                while (x < colorTree.size())
                {
                    if (colorTree.get(x).getText().equals("abstract") || colorTree.get(x).getText().equals("interface") || colorTree.get(x).getText().equals("this") || colorTree.get(x).getText().equals("THROWS_CLAUSE") || colorTree.get(x).getText().equals("char") || colorTree.get(x).getText().equals("IMPLEMENTS_CLAUSE") || colorTree.get(x).getText().equals("EXTENDS_CLAUSE") ||colorTree.get(x).getText().equals("final") || colorTree.get(x).getText().equals("final") || colorTree.get(x).getText().equals("static") || colorTree.get(x).getText().equals("byte") || colorTree.get(x).getText().equals("break") || colorTree.get(x).getText().equals("try") || colorTree.get(x).getText().equals("catch") || colorTree.get(x).getText().equals("false") || colorTree.get(x).getText().equals("true") || colorTree.get(x).getText().equals("while") || colorTree.get(x).getText().equals("for") || colorTree.get(x).getText().equals("if") || colorTree.get(x).getText().equals("boolean") || colorTree.get(x).getText().equals("new") || colorTree.get(x).getText().equals("int") || colorTree.get(x).getText().equals("import") || colorTree.get(x).getText().equals("return") || colorTree.get(x).getText().equals("class") || colorTree.get(x).getText().equals("extends") || colorTree.get(x).getText().equals("public") || colorTree.get(x).getText().equals("private") || colorTree.get(x).getText().equals("package"))
                    {
                        int length = colorTree.get(x).getText().length();
                        if(colorTree.get(x).getText().equals("THROWS_CLAUSE"))
                        {
                            length = 6;
                        }
                        if(colorTree.get(x).getText().equals("EXTENDS_CLAUSE"))
                        {
                            length = 7;
                        }
                        if(colorTree.get(x).getText().equals("IMPLEMENTS_CLAUSE"))
                        {
                            length = 10;
                        }
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(0);
                        obj.setG(0);
                        obj.setB(225);
                        obj.setColor("Blue");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getText().equals("STATIC_ARRAY_CREATOR"))
                    {
                        int length = 3;
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(0);
                        obj.setG(0);
                        obj.setB(225);
                        obj.setColor("Blue");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getText().equals("throw") && colorTree.get(x).getType() == 96)
                    {
                        int length = colorTree.get(x).getText().length();
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(0);
                        obj.setG(0);
                        obj.setB(225);
                        obj.setColor("Blue");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getType() == 164 && colorTree.get(x-1).getText().equals("out") && ((x-1) > -1) && colorTree.get(x-1).getText().equals("."))
                    {
                        int length = 3;
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(0);
                        obj.setG(0);
                        obj.setB(225);
                        obj.setColor("Blue");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getType() == 164 && !colorTree.get(x).getText().equals("String")&& !colorTree.get(x).getText().equals("System") && (x-1) > 1 && !colorTree.get(x-1).getText().equals("MODIFIER_LIST") && !colorTree.get(x-1).getText().equals("import") && !colorTree.get(x-1).getText().equals("FORMAL_PARAM_LIST") && !colorTree.get(x-1).getText().equals("LOCAL_MODIFIER_LIST") && !colorTree.get(x-1).getText().equals("EXP") && !colorTree.get(x-1).getText().equals("FUNCTION_METHOD_DECL") && !colorTree.get(x-1).getText().equals("VOID_METHOD_DECL") && !colorTree.get(x-1).getText().equals("class") && !colorTree.get(x-1).getText().equals(".") && !colorTree.get(x-1).getText().equals("@"))
                    {
                        int length = colorTree.get(x).getText().length();
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(0);
                        obj.setG(201);
                        obj.setB(87);
                        obj.setColor("Green");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getText().equals("VOID_METHOD_DECL"))
                    {
                        int length = 4;
                        int startPosition = colorTree.get(x+1).getCharPositionInLine() - 5;
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x+1).getLine());
                        obj.setR(0);
                        obj.setG(0);
                        obj.setB(225);
                        obj.setColor("Blue");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getText().equals("out"))
                    {
                        int length = colorTree.get(x).getText().length();;
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(0);
                        obj.setG(201);
                        obj.setB(87);
                        obj.setColor("Green");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getType() == 164 && (x-1) > -1 && colorTree.get(x-1).getText().equals("VOID_METHOD_DECL"))
                    {
                        int length = colorTree.get(x).getText().length();;
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(3);
                        obj.setG(3);
                        obj.setB(3);
                        obj.setColor("Black");
                        key.add(obj);
                    }
                    if (colorTree.get(x).getText().charAt(0) == '\"')
                    {
                        int length = colorTree.get(x).getText().length();
                        int startPosition = colorTree.get(x).getCharPositionInLine();
                        keyWords obj = new keyWords();
                        obj.setColumnNum(startPosition);
                        obj.setLength(length);
                        obj.setLineNum(colorTree.get(x).getLine());
                        obj.setR(225);
                        obj.setG(171);
                        obj.setB(36);
                        obj.setColor("Chocolate 1");

                        key.add(obj);
                    }
                    x++;
                }//while
                ki = new keyWords();
                key = ki.sort(key);
                sa = "";
                counte = 0;
                total = 0;
                df = 0;
                while((sa = b.readLine()) != null)
                {
                    if(df < key.size() && counte==key.get(df).getLineNum()-1)
                    {
                        Color color = new Color(key.get(df).getR(),key.get(df).getG(),key.get(df).getB());
                        Style style;
                        style = beforeTextPane.addStyle(key.get(df).getColor(), null);
                        StyleConstants.setForeground(style, color);
                        doc.setCharacterAttributes(total+key.get(df).getColumnNum(), key.get(df).getLength(), beforeTextPane.getStyle(key.get(df).getColor()), true);

                        df++;
                        while(df < key.size() && key.get(df).getLineNum()== key.get(df-1).getLineNum())
                        {
                            color = new Color(key.get(df).getR(),key.get(df).getG(),key.get(df).getB());
                            style = beforeTextPane.addStyle(key.get(df).getColor(), null);
                            StyleConstants.setForeground(style, color);
                            doc.setCharacterAttributes(total+key.get(df).getColumnNum(), key.get(df).getLength(), beforeTextPane.getStyle(key.get(df).getColor()), true);
                            df++;
                        }//while
                    }//if
                    counte += 1;
                    if(sa.equals(""))
                    {
                        total += 1;
                    }
                    else
                    {
                        total += sa.length()+1;
                    }
                }//while
            }//else
        }//try
        catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "IO Exception", JOptionPane.ERROR_MESSAGE);
        }
        
        beforeTextPane.getDocument().addDocumentListener(new DocumentListener() 
        {
            public String getText()
            {
                int caretPosition = beforeTextPane.getDocument().getLength();
                Element root = beforeTextPane.getDocument().getDefaultRootElement();
                String text = "1" + System.getProperty("line.separator");
                for(int i = 2; i < root.getElementIndex( caretPosition ) + 2; i++)
                {
                    text += i + System.getProperty("line.separator");
                }
                return text;
            }

            @Override
            public void insertUpdate(DocumentEvent evt) 
            {
                searcher.search(word);
                beforeNumbers.setText(getText());
            }

            @Override
            public void removeUpdate(DocumentEvent evt) 
            {
                searcher.search(word);
                beforeNumbers.setText(getText());
            }

            @Override
            public void changedUpdate(DocumentEvent evt) 
            {
                searcher.search(word);
                beforeNumbers.setText(getText());
            }
        });

        afterTextPane.getDocument().addDocumentListener(new DocumentListener() 
        {
            public String getText()
            {
                int caretPosition = afterTextPane.getDocument().getLength();
                Element root = afterTextPane.getDocument().getDefaultRootElement();
                String text = "1" + System.getProperty("line.separator");
                for(int i = 2; i < root.getElementIndex( caretPosition ) + 2; i++)
                {
                    text += i + System.getProperty("line.separator");
                }
                return text;
            }

            @Override
            public void insertUpdate(DocumentEvent evt) 
            {
                searcher.search(word);
                afterNumbers.setText(getText());
            }

            @Override
            public void removeUpdate(DocumentEvent evt) 
            {
                searcher.search(word);
                afterNumbers.setText(getText());
            }

            @Override
            public void changedUpdate(DocumentEvent evt) 
            {
                searcher.search(word);
                afterNumbers.setText(getText());
            }
        });

        if (view.equals("0"))
        {
            pane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
            
            beforePane.setViewportView(beforeTextPane);

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(beforePanel);
            beforePanel.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(beforeLabel)
                    .addContainerGap(66, Short.MAX_VALUE))
                .addComponent(beforePane, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(beforeLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(beforePane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
            );

            pane.setLeftComponent(beforePanel);

            afterPane.setViewportView(afterTextPane);

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(afterPanel);
            afterPanel.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(afterLabel)
                    .addContainerGap(332, Short.MAX_VALUE))
                .addComponent(afterPane, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(afterLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(afterPane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
            );

            pane.setRightComponent(afterPanel);
        }
        else if (view.equals("1"))
        {
            pane.setOrientation(JSplitPane.VERTICAL_SPLIT);
            
            beforePane.setViewportView(beforeTextPane);

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(beforePanel);
            beforePanel.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(beforeLabel)
                    .addContainerGap(66, Short.MAX_VALUE))
                .addComponent(beforePane, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(beforeLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(beforePane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
            );

            pane.setLeftComponent(beforePanel);

            afterPane.setViewportView(afterTextPane);

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(afterPanel);
            afterPanel.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(afterLabel)
                    .addContainerGap(332, Short.MAX_VALUE))
                .addComponent(afterPane, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(afterLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(afterPane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
            );

            pane.setRightComponent(afterPanel);
        }
        else
        {
            pane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
            
            beforePane.setViewportView(beforeTextPane);

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(beforePanel);
            beforePanel.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(beforeLabel)
                    .addContainerGap(66, Short.MAX_VALUE))
                .addComponent(beforePane, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(beforeLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(beforePane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
            );

            pane.setLeftComponent(beforePanel);

            afterPane.setViewportView(afterTextPane);

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(afterPanel);
            afterPanel.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(afterLabel)
                    .addContainerGap(332, Short.MAX_VALUE))
                .addComponent(afterPane, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(afterLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(afterPane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
            );

            pane.setRightComponent(afterPanel);
        }
        return pane;
    }
    
    /*
     * The Find Frame.
     */
    void findFrame()
    {
        JFrame findFrame = new JFrame();
        
        JLabel findLabel = new JLabel();
        final JTextField findTextField = new JTextField();
        JButton findButton = new JButton();

        findTextField.setText(word);
        
        findFrame.setTitle("Find");

        findLabel.setText("Find:");
        findLabel.setToolTipText("Type a word to find.");

        findTextField.setToolTipText("Type a word to find.");

        findButton.setText("Find");
        findButton.setToolTipText("Type a word to find.");
        findButton.addActionListener(new java.awt.event.ActionListener() 
        {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                findButtonActionPerformed(evt);
            }

            /**
            * Find Button.
            * @param evt 
            */
            private void findButtonActionPerformed(ActionEvent evt) 
            {
                word = findTextField.getText().trim();

                String[] fileNames = new String[jTabbedPane1.getTabCount()];

                for (int x = 0; x < fileNames.length; x++)
                {
                    fileNames[x] = jTabbedPane1.getTitleAt(x);
                }

                jTabbedPane1.removeAll();

                for (int x = 0; x < fileNames.length; x++)
                {
                    if (resultList.size() > 0 && resultList.get(1) != null)
                    {
                        jTabbedPane1.addTab(fileNames[x], display(fileNames[x], fileNames[x], view));
                    }
                    else
                    {
                        jTabbedPane1.addTab(fileNames[x], display(fileNames[x], "", view));
                    }
                }
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(findFrame.getContentPane());
        findFrame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(findButton, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(findLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(findTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(findLabel)
                    .addComponent(findTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(findButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        
        screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        findFrame.setBounds((screenSize.width-192)/2, (screenSize.height-111)/2, 192, 111);
        findFrame.setVisible(true);
    }
      
    /**
     * Checks if the tab is already open.
     * @param className. The class name to check.
     * @return boolean. Returns true if it is open.
     */
    boolean isTabOpen(String className)
    {
        int x = 0;
        boolean alreadyOpen = false;
        while (x < jTabbedPane1.getTabCount())
        {
            if (jTabbedPane1.getTitleAt(x).equals(className))
            {
                alreadyOpen = true;
            }
            x++;
        }
        return alreadyOpen;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) 
    {
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            {
                try 
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }
                catch (ClassNotFoundException ex) 
                {
                    JOptionPane.showMessageDialog(null, ex.toString(), "Class Not Found Exception", JOptionPane.ERROR_MESSAGE);
                }
                catch (InstantiationException ex) 
                {
                    JOptionPane.showMessageDialog(null, ex.toString(), "Instantiation Exception", JOptionPane.ERROR_MESSAGE);
                }
                catch (IllegalAccessException ex) 
                {
                    JOptionPane.showMessageDialog(null, ex.toString(), "Illegal Access Exception", JOptionPane.ERROR_MESSAGE);
                }
                catch (UnsupportedLookAndFeelException ex) 
                {
                    JOptionPane.showMessageDialog(null, ex.toString(), "Unsupported Look AndFeel Exception", JOptionPane.ERROR_MESSAGE);
                }
                new GUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTree jTree1;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables
}